package com.c2soft.nepalcmx.webapp;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
import com.c2soft.nepalcmx.webapp.security.*;

/**
 * The Class SecurityWebApplicationInitializer.
 */
public class SecurityInitializer extends
		AbstractSecurityWebApplicationInitializer {

	/**
	 * Instantiates a new security web application initializer.
	 */
	

	public SecurityInitializer() {
		super(WebSecurityConfig.class);
	}

	@Configuration
	@EnableWebMvcSecurity
	@Import({ DataSourceConfig.class })
	public static class WebSecurityConfig extends WebSecurityConfigurerAdapter {

		@Autowired
		private AuthenticationSuccessHandler successHandler;
		
		@Override
		public void configure(WebSecurity web) throws Exception {
			web.ignoring().antMatchers("/resources/**");

		}

		@Override
		protected void configure(HttpSecurity http) throws Exception {
			http.csrf().disable();
			http.authorizeRequests()
					.antMatchers("/", "/index**","/forgotPwd**","/user/forgotPwd**","/order/**","/netPosition/**","/ProfitLoss/**","/ProfitLoss1/**","/ProfitLoss2/**",
							"/user/UpdatePwd1/**","/user/UpdateTransPwd/**","/netPosition1/**","/netPosition2/**",
							"/tradeBook/**","/prevtradeBook/**","/mock/clientpage1/**","/mock/brokerpage/**","/mock/ExchangePass/**",
							"/mock/bank1/**","/mock/brokerbank/**","/tradeBook1/**","/tradeBook2/**","/prevtradeBook1/**",
							"/prevtradeBook2/**","/user/**","/broker/**","/userList/**","/brokerList/**","/bank/**","/order/buy/**")
					.permitAll()
					.antMatchers("/css/**", "/img/**", "/images/**", "/js/**",
							"/j_spring_security_check").permitAll()
					.antMatchers("/home**").hasRole("BROKER")
					.antMatchers("/clientpage**").hasRole("USER")
					.antMatchers("/exchangelogin**").hasRole("EXCHANGE").anyRequest()
					.authenticated().and().formLogin().loginPage("/login")
					.successHandler( successHandler)
					.permitAll().and().logout().logoutUrl("/logout")
					.logoutSuccessUrl("/index").permitAll();
		}

		@Autowired
		public void configureGlobal(DataSource dataSource,AuthenticationManagerBuilder auth)
				throws Exception {
			auth.jdbcAuthentication()
					.dataSource(dataSource)
					.usersByUsernameQuery(
							"select user_id,password, enabled from users where user_id=?")
					.authoritiesByUsernameQuery(
							" select u.user_id, ur.authority from users u, user_roles ur where u.user_id = ur.user_id and u.user_id =?");
		}

	}
}
